package com.flyme.module.base.provider.controller;

import cn.hutool.json.JSONUtil;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysVipTypeRemoteService;
import com.flyme.module.base.client.entity.SysVipType;
import com.flyme.module.base.client.entity.SysVipconfig;
import com.flyme.module.base.provider.service.SysVipTypeService;
import com.flyme.module.base.provider.service.SysVipconfigService;
import com.flyme.module.exam.client.vo.ExamItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会员类型管理
 *
 * @author zyf
 */
@Api(tags = "会员类型管理")
@RestController
public class SysVipTypeController extends BaseController<SysVipTypeService, SysVipType> implements SysVipTypeRemoteService {

    @Autowired
    private SysVipTypeService sysVipTypeService;

    @Autowired
    private SysVipconfigService vipconfigService;


    @ApiOperation(value = "列表", notes = "列表")
    @Override
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysVipType sysVipType) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("id", "vipName", "vipCode", "vipTime", "vipPrice", "vipImg", "iosPrice", "createTime", "updateTime", "updateUser");
        PqGrid pqGrid = sysVipTypeService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加", notes = "添加")
    @Override
    public ResultBody add(@ModelAttribute SysVipType sysVipType) {
        Boolean tag = sysVipTypeService.add(sysVipType);
        return ResultBody.success("添加成功").setData(tag);
    }

    @ApiOperation(value = "更新", notes = "更新")
    @Override
    public ResultBody update(@ModelAttribute SysVipType sysVipType) {
        Boolean tag = sysVipTypeService.update(sysVipType);
        return ResultBody.success("更新成功").setData(tag);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @Override
    public ResultBody delByIds(@RequestParam String[] ids) {
        Boolean tag = sysVipTypeService.delByIds(ids);
        return ResultBody.success(tag);
    }

    @Override
    public ResultBody getById(@PathVariable Long id) {
        SysVipType vipType = sysVipTypeService.getById(id);
        List<ExamItem> items = vipconfigService.selectList(id);
        vipType.setVipConfigItems(JSONUtil.toJsonStr(items));
        return ResultBody.success(vipType);
    }

}
